﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SQLite;

namespace KeyboardImageViewer
{
    public static class DatabaseConfigAccess
    {
        private static Dictionary<string, string> configDefaults;

        static DatabaseConfigAccess()
        {
            configDefaults = new Dictionary<string, string>();
            configDefaults.Add("Version", Utilities.CurrentDatabaseVersion.ToString());
            configDefaults.Add("UpdatesEnabled", "True");
            configDefaults.Add("UpdateInProgress", "False");
            configDefaults.Add("UpdateVersion", string.Empty);
            configDefaults.Add("UpdateChangelogLocation", string.Empty);
            configDefaults.Add("UpdateInstallerLocation", string.Empty);
            configDefaults.Add("LastSettingsTab", "1");
            configDefaults.Add("TrackDuplicates", "False");
            configDefaults.Add("TrackImageDuplicates", "False");
            configDefaults.Add("SearchFileName", "True");
            configDefaults.Add("SearchTags", "True");
            configDefaults.Add("StartingListId", "1");

            AddWindowSizeDefaults(configDefaults, "Main", "Import", "Duplicate", "ViewLists", "Settings", "EditKeys", "AllDuplicates", "ImageDetail");

            configDefaults.Add("ExpandedDirectories", string.Empty);
            configDefaults.Add("DeleteDuplicatesOnImport", "False");
            configDefaults.Add("FullscreenStartSetting", "0");
            configDefaults.Add("FullscreenStart", "True");
            configDefaults.Add("OverlayInfobar", "True");
        }

        public static Dictionary<string, string> ConfigDefaults
        {
            get
            {
                return configDefaults;
            }
        }

        public static bool GetConfigBool(string configName, SQLiteConnection connection)
        {
            return bool.Parse(GetConfigString(configName, connection));
        }

        public static double GetConfigDouble(string configName, SQLiteConnection connection)
        {
            return double.Parse(GetConfigString(configName, connection));
        }

        public static int GetConfigInt(string configName, SQLiteConnection connection)
        {
            return int.Parse(GetConfigString(configName, connection));
        }

        public static string GetConfigString(string configName, SQLiteConnection connection)
        {
            using (SQLiteCommand command = new SQLiteCommand("SELECT value FROM settings WHERE name = '" + configName + "'", connection))
            {
                using (SQLiteDataReader reader = command.ExecuteReader())
                {
                    if (reader.Read())
                    {
                        return reader.GetString(0);
                    }
                }
            }

            // The setting does not exist in the DB, we need to insert the default value.

            if (configDefaults.ContainsKey(configName))
            {
                AddConfigValue(configName, configDefaults[configName], connection);

                return configDefaults[configName];
            }

            throw new ArgumentException("Config value not found: " + configName, "configName");
        }

        private static void AddConfigValue(string configName, string configValue, SQLiteConnection connection)
        {
            using (SQLiteCommand settingsInsert = new SQLiteCommand(connection))
            {
                var settingsName = new SQLiteParameter();
                var settingsValue = new SQLiteParameter();

                settingsInsert.CommandText = "INSERT INTO settings (name, value) VALUES (?, ?)";
                settingsInsert.Parameters.Add(settingsName);
                settingsInsert.Parameters.Add(settingsValue);

                settingsName.Value = configName;
                settingsValue.Value = configValue;

                settingsInsert.ExecuteNonQuery();
            }
        }

        public static void SetConfigValue(string configName, bool value, SQLiteConnection connection)
        {
            SetConfigValue(configName, value.ToString(), connection);
        }

        public static void SetConfigValue(string configName, double value, SQLiteConnection connection)
        {
            SetConfigValue(configName, value.ToString(), connection);
        }

        public static void SetConfigValue(string configName, int value, SQLiteConnection connection)
        {
            SetConfigValue(configName, value.ToString(), connection);
        }

        public static void SetConfigValue(string configName, string configValue, SQLiteConnection connection)
        {
            using (SQLiteCommand command = new SQLiteCommand("UPDATE settings SET value = \"" + configValue + "\" WHERE name = '" + configName + "'", connection))
            {
                if (command.ExecuteNonQuery() == 0)
                {
                    // If the setting did not exist, add it
                    AddConfigValue(configName, configValue, connection);
                }
            }
        }

        private static void AddWindowSizeDefaults(Dictionary<string, string> configDefaults, params string[] windowNames)
        {
            foreach (string windowName in windowNames)
            {
                configDefaults.Add(windowName + "Width", "0");
                configDefaults.Add(windowName + "Height", "0");
            }
        }
    }
}
